//
//  FJDetailViewController.swift
//  FruitJuice
//
//  Created by Rae on 2023/12/26.
//

import UIKit

class FJDetailViewController: FJBaseViewController, SAAnimationProtocol {

    private let backButton          = c_make_ex(UIButton.self)
        .image("FJ_Profile_back").end()
    private let moreButton          = c_make_ex(UIButton.self)
        .image("FJ_Home_Detail_more").end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.tableFooterView = UIView(frame: .zero)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.estimatedRowHeight = 630
        tableView.register(FJHomeDetailTableViewCell.self,FJDetailCommitCell.self)

        return tableView
    }()
    
    private var commentList:[InfoUtil.commentDta] = []
    
    var model:InfoUtil.room?{
        didSet{
            
            loadCommentListData()
        }
    }
    
    private func loadCommentListData(){

        RT.getCommentList(houseId: (model?.id)!) { list in
            self.commentList.removeAll()
            for item in list {
                if let it = item {
                    self.commentList.append(it)
                }
            }
//            if let delArr = UserDefaults.standard.value(forKey: self.itemModel.icon) {
//                let temp = delArr as! [String]
//                for del in temp {
//                    self.commentList.removeAll(where: {$0.id == del})
//                }
//            }
//            if self.commentList.count == 0 {
//                self.isCommentEmpty = true
//            }else {
//                self.isCommentEmpty = false
//            }
            self.tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigation.bar.isHidden = true
        
        view.subviews{
            tableView
            backButton
            moreButton
        }
        
        backButton.top(.statusHeight + 7).leading(15).size(30)
        moreButton.top(.statusHeight + 7).trailing(15).size(30)
        tableView.leading(0).trailing(0).top(.navi_height).bottom(.safe_bottom)
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        moreButton.addTarget(self, action: #selector(clickReportButton), for: .touchUpInside)
    }
    
    @objc private func clickBackButton(){
        self.navigationController?.popViewController(animated: true)
    }

    @objc private func clickReportButton(){
        let reportV = FJReportView()
        reportV.otherId = model?.userId ?? ""
        self.saAnimate.animation(show: reportV)
    }
}

extension FJDetailViewController: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return commentList.count + 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.row == 0 {
            let cell = tableView.cell(FJHomeDetailTableViewCell.self, indexPath: indexPath)
            cell.model = model
            cell.sentCommitClosure = { [weak self] msg in
                RT.sentCommit(houseId: (self?.model?.id)!, msg: msg, userId: USEINFOMODEL.id ?? "") { success in
                    "Success".toast_success
                    self?.loadCommentListData()
                }
            }
            return cell
        }else{
            let cell = tableView.cell(FJDetailCommitCell.self, indexPath: indexPath)
            cell.model = commentList[indexPath.row - 1]
            cell.blackClosure = { [weak self] userId in
                let reportV = FJReportView()
                reportV.otherId = userId
                self?.saAnimate.animation(show: reportV)
            }
            return cell
        }
    }

}
